home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr48 / pas_0593.zip / PASCAL45.FAQ < prev    next >
Text File  |  1993-05-27  |  4KB  |  96 lines

  1. ─ Fido Pascal Conference ────────────────────────────────────────────── PASCAL ─
  2. Msg  : 55 of 345                                                                
  3. From : Max Maischein                       2:249/6.17           20 May 93  13:38 
  4. To   : All                                                                       
  5. Subj : Pascal FAQs Part IV / II                                               
  6. ────────────────────────────────────────────────────────────────────────────────
  7. PASCAL.FAQ             Frequently asked questions about Pascal
  8.  
  9. The aim of this document is to give answers to frequently asked
  10. questions in the pascal echo. Thumb rules before asking for help are to
  11. look in the manuals and in the online help first. Many problems can be
  12. solved by just looking into either / both of them. Here are some
  13. topics, that come very often in the Pascal Echo.
  14.  
  15.                                 Part IV
  16.        #7: Typed and untyped files (II)
  17. Q3: How do I delete a line from a text file ?
  18. Q4: How can I skip a header in my file ?
  19. Q5: How can I speed up my file I/O ?
  20.  
  21. ---------------------------------------------------------------------
  22.                       #7: Typed and untyped files (II)
  23. Q3: How do I delete a line from a text file ?
  24.  
  25. A3: There is only one way to delete a line from a text file. You will
  26.     have to read the source file line by line, check if a line has to be
  27.     removed and if not, write the line out. Note that TP has a limit of
  28.     255 chars per line, because strings can only hold up to 255 chars.
  29.     If you need to accept more than 255 chars per line or have unbound
  30.     text from a word editor, this won't work.
  31.  
  32.     Var Infile, OutFile : Text;
  33.         S : String;
  34.  
  35.       Function RemoveLine( S : String ) : Boolean;
  36.       Begin
  37.         RemoveLine := Pos( 'MAX', S ) <> 0;
  38.       End;
  39.  
  40.     Begin
  41.       OpenFiles;
  42.       Repeat
  43.         ReadLn( Infile, S );
  44.         If not RemoveLine( S )
  45.           then WriteLn( OutFile, S );
  46.       Until EOF( Infile );
  47.       CloseFiles;
  48.     End.
  49.  
  50. Q4: How can I skip a header in my typed file.
  51. A4: There _are_ some dirty ways to skip a header that has not the size
  52.     of the other records in a typed file, but I recommend that you use
  53.     untyped files and BlockRead() / BlockWrite() then, because the ways
  54.     to skip some bytes ini a file work around some stuff of TP. If you
  55.     are sure that the proposed skip code works, use it at your own risk.
  56.  
  57. Q5: How can I speed up my file I/O ?
  58. A5: Use buffers. Today, most machines have a cache installed, but this
  59.     dosen't mean, that read / write to / from disk are fast. For text
  60.     file IO, you should use SetTextBuf, this will automatically enable
  61.     a RAM buffer that minimizes disk accesses with no further hassle for
  62.     you. For other files, you will have to read a part of the file in a
  63.     buffer in RAM and then process it, this will introduce more
  64.     possibilities for errors in your program. You should implement
  65.     buffers as the last part in your program so you can be sure that the
  66.     rest is running correctly, because if you are working with untyped
  67.     files, fencepost errors ( off-by-one-errors ) are frequent !
  68.  
  69.     Pseudocode for using buffers with untyped files :
  70.  
  71.     Const cBufSize = 65520;
  72.     Var Buffer : Pointer to Array[ 0..65519 ];
  73.         BytesRead : Word;
  74.         BufSize : Word;
  75.  
  76.     BufSize := Minimum( cBufSize, MaxAvail );
  77.     GetMem( Buffer, BufSize );
  78.  
  79.     While BytesRead <> 0 do
  80.       Begin
  81.         BlockRead( Infile, Buffer^, BufSize, BytesRead );
  82.         ProcessBytesInBuffer( Buffer^, BytesRead );
  83.         BlockWrite( OutFile, Buffer^, BytesRead );
  84.       End;
  85.  
  86. ---------------------------------------------------------------------
  87.  
  88. -max
  89.  
  90. This file is copyrighted by Max Maischein, 2:249/6.17.
  91. No part thereof may be printed without my written permission.
  92.  
  93. ---
  94.  * Origin: Arrays of Pointers to Arrays of Pointers to ... (2:249/6.17)
  95.  
  96.